package com.cskaoyan.advice;

import com.cskaoyan.bean.vo.BaseRespVo;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: harveyliu
 * @description: 用来处理异常
 * @time: 2021/7/12 22:02
 */
// TODO 需要继续改进  重点处理空指针那个  能让其跳转到登录页面
@ControllerAdvice
public class ExceptionControllerAdvice {
    /**
     * @Author harvey
     * @description: 注意该方法用浏览器的get请求的访问不到的，因为没有给一个请求头的token
     * @Date 2021/7/13 10:37
     * url:
     */
    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public BaseRespVo handlerAuthorException(AuthorizationException e) {
        return BaseRespVo.fail("没有登录或者没有权限");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public BaseRespVo handlerAuthorException(IncorrectCredentialsException e) {
        return BaseRespVo.fail("帐号密码错误");
    }

    /**
     * @Author harvey
     * @description: 场景描述：当用户已经登录的情况下，重启服务器，在页面点击logout按钮
     *              会发生空指针异常  具体体现在CustomAspect的71行代码上
     *              对其进行提示登录的处理
     * @Date 2021/7/12 22:17
     */
    /*@ExceptionHandler(NullPointerException.class)

//    @ExceptionHandler(NullPointerException.class)

    @ResponseBody
    public BaseRespVo handlerNullPointerException(NullPointerException e) {
        return BaseRespVo.fail("没有登录!");
    }*/
}
